package rs.graphics;// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 

import rs.stream.Packet;
import rs.stream.JagexArchive;

import java.util.Random;

public final class RSFont extends DrawingArea {

    public RSFont(boolean flag, String file, JagexArchive titleArchive) {
        glyphPixels = new byte[256][];
        glyphWidth = new int[256];
        glyphHeight = new int[256];
        horizontalKerning = new int[256];
        verticalKerning = new int[256];
        charEffectiveWidth = new int[256];
        random = new Random();
        isStrikethrough = false;
        Packet fileStream = new Packet(titleArchive.getDataForName(file + ".dat"));
        Packet indexStream = new Packet(titleArchive.getDataForName("index.dat"));
        indexStream.currentOffset = fileStream.readUnsignedWord() + 4;
        int k = indexStream.readUnsignedByte();
        if (k > 0)
            indexStream.currentOffset += 3 * (k - 1);
        for (int l = 0; l < 256; l++) {
            horizontalKerning[l] = indexStream.readUnsignedByte();
            verticalKerning[l] = indexStream.readUnsignedByte();
            int width = glyphWidth[l] = indexStream.readUnsignedWord();
            int height = glyphHeight[l] = indexStream.readUnsignedWord();
            int k1 = indexStream.readUnsignedByte();
            int l1 = width * height;
            glyphPixels[l] = new byte[l1];
            if (k1 == 0) {
                for (int i2 = 0; i2 < l1; i2++)
                    glyphPixels[l][i2] = fileStream.readSignedByte();

            } else if (k1 == 1) {
                for (int j2 = 0; j2 < width; j2++) {
                    for (int l2 = 0; l2 < height; l2++)
                        glyphPixels[l][j2 + l2 * width] = fileStream.readSignedByte();

                }

            }
            if (height > charHeight && l < 128)
                charHeight = height;
            horizontalKerning[l] = 1;
            charEffectiveWidth[l] = width + 2;
            int k2 = 0;
            for (int i3 = height / 7; i3 < height; i3++)
                k2 += glyphPixels[l][i3 * width];

            if (k2 <= height / 7) {
                charEffectiveWidth[l]--;
                horizontalKerning[l] = 0;
            }
            k2 = 0;
            for (int j3 = height / 7; j3 < height; j3++)
                k2 += glyphPixels[l][(width - 1) + j3 * width];

            if (k2 <= height / 7)
                charEffectiveWidth[l]--;
        }

        if (flag) {
            charEffectiveWidth[32] = charEffectiveWidth[73];
        } else {
            charEffectiveWidth[32] = charEffectiveWidth[105];
        }
    }

    public void drawTextHRightVTop(String text, int x, int y, int color) {
        drawTextHLeftVTop(y, text, color, x - getTextWidth(text));
    }

    public void drawTextHMidVTop(int color, String text, int y, int x) {
        drawTextHLeftVTop(color, text, y, x - getTextWidth(text) / 2);
    }

    public void drawShadowTextHMidVTop(int textColor, int x_pos, String displayText, int y_pos, boolean displayIt) {
        drawShadowTextHLeftVTop(displayIt, x_pos - getFormattedStringWith(displayText) / 2, textColor, displayText, y_pos);
    }

    public int getFormattedStringWith(String s) {
        if (s == null)
            return 0;
        int j = 0;
        for (int k = 0; k < s.length(); k++)
            if (s.charAt(k) == '@' && k + 4 < s.length() && s.charAt(k + 4) == '@')
                k += 4;
            else
                j += charEffectiveWidth[s.charAt(k)];

        return j;
    }

    public int getTextWidth(String s) {
        if (s == null)
            return 0;
        int j = 0;
        for (int k = 0; k < s.length(); k++)
            j += charEffectiveWidth[s.charAt(k)];
        return j;
    }

    public void drawTextHLeftVTop(int color, String text, int y, int x) {
        if (text == null)
            return;
        y -= charHeight;
        for (int i1 = 0; i1 < text.length(); i1++) {
            char c = text.charAt(i1);
            if (c != ' ')
                drawGlyph(glyphPixels[c], x + horizontalKerning[c], y + verticalKerning[c], glyphWidth[c], glyphHeight[c], color);
            x += charEffectiveWidth[c];
        }
    }

    public void drawTextHRMidVTopWaving(int color, String text, int x, int wavePart, int y) {
        if (text == null)
            return;
        x -= getTextWidth(text) / 2;
        y -= charHeight;
        for (int i1 = 0; i1 < text.length(); i1++) {
            char c = text.charAt(i1);
            if (c != ' ')
                drawGlyph(glyphPixels[c], x + horizontalKerning[c], y + verticalKerning[c] + (int) (Math.sin((double) i1 / 2D + (double) wavePart / 5D) * 5D), glyphWidth[c], glyphHeight[c], color);
            x += charEffectiveWidth[c];
        }

    }

    public void drawTextHRMidVTopWaving2(int x, String text, int wavePart, int y, int color) {
        if (text == null)
            return;
        x -= getTextWidth(text) / 2;
        y -= charHeight;
        for (int i1 = 0; i1 < text.length(); i1++) {
            char c = text.charAt(i1);
            if (c != ' ')
                drawGlyph(glyphPixels[c], x + horizontalKerning[c] + (int) (Math.sin((double) i1 / 5D + (double) wavePart / 5D) * 5D), y + verticalKerning[c] + (int) (Math.sin((double) i1 / 3D + (double) wavePart / 5D) * 5D), glyphWidth[c], glyphHeight[c], color);
            x += charEffectiveWidth[c];
        }

    }

    public void drawTextHRMidVTopShaking(int wavePart, String text, int wavePart2, int y, int x, int color) {
        if (text == null)
            return;
        double d = 7D - (double) wavePart / 8D;
        if (d < 0.0D)
            d = 0.0D;
        x -= getTextWidth(text) / 2;
        y -= charHeight;
        for (int k1 = 0; k1 < text.length(); k1++) {
            char c = text.charAt(k1);
            if (c != ' ')
                drawGlyph(glyphPixels[c], x + horizontalKerning[c], y + verticalKerning[c] + (int) (Math.sin((double) k1 + (double) wavePart2) * d), glyphWidth[c], glyphHeight[c], color);
            x += charEffectiveWidth[c];
        }

    }

    public void drawShadowTextHLeftVTop(boolean shadowIt, int x, int color, String text, int y) {
        isStrikethrough = false;
        int l = x;
        if (text == null)
            return;
        y -= charHeight;
        for (int i1 = 0; i1 < text.length(); i1++)
            if (text.charAt(i1) == '@' && i1 + 4 < text.length() && text.charAt(i1 + 4) == '@') {
                int j1 = getColorByName(text.substring(i1 + 1, i1 + 4));
                if (j1 != -1)
                    color = j1;
                i1 += 4;
            } else {
                char c = text.charAt(i1);
                if (c != ' ') {
                    if (shadowIt)
                        drawGlyph(glyphPixels[c], x + horizontalKerning[c] + 1, y + verticalKerning[c] + 1, glyphWidth[c], glyphHeight[c], 0);
                    drawGlyph(glyphPixels[c], x + horizontalKerning[c], y + verticalKerning[c], glyphWidth[c], glyphHeight[c], color);
                }
                x += charEffectiveWidth[c];
            }
        if (isStrikethrough)
            drawHLine(y + (int) ((double) charHeight * 0.69999999999999996D), 0x800000, x - l, l);
    }

    public void drawShadowedTextRight(int x, int color, String s, int seed, int y) {
        if (s == null)
            return;
        random.setSeed(seed);
        int j1 = 192 + (random.nextInt() & 0x1f);
        y -= charHeight;
        for (int k1 = 0; k1 < s.length(); k1++)
            if (s.charAt(k1) == '@' && k1 + 4 < s.length() && s.charAt(k1 + 4) == '@') {
                int l1 = getColorByName(s.substring(k1 + 1, k1 + 4));
                if (l1 != -1)
                    color = l1;
                k1 += 4;
            } else {
                char c = s.charAt(k1);
                if (c != ' ') {
                    drawShadowedChar(192, x + horizontalKerning[c] + 1, glyphPixels[c], glyphWidth[c], y + verticalKerning[c] + 1, glyphHeight[c], 0);
                    drawShadowedChar(j1, x + horizontalKerning[c], glyphPixels[c], glyphWidth[c], y + verticalKerning[c], glyphHeight[c], color);
                }
                x += charEffectiveWidth[c];
                if ((random.nextInt() & 3) == 0)
                    x++;
            }

    }

    private int getColorByName(String s) {
        if (s.equals("red"))
            return 0xff0000;
        if (s.equals("gre"))
            return 65280;
        if (s.equals("blu"))
            return 255;
        if (s.equals("yel"))
            return 0xffff00;
        if (s.equals("cya"))
            return 65535;
        if (s.equals("mag"))
            return 0xff00ff;
        if (s.equals("whi"))
            return 0xffffff;
        if (s.equals("bla"))
            return 0;
        if (s.equals("lre"))
            return 0xff9040;
        if (s.equals("dre"))
            return 0x800000;
        if (s.equals("dbl"))
            return 128;
        if (s.equals("or1"))
            return 0xffb000;
        if (s.equals("or2"))
            return 0xff7000;
        if (s.equals("or3"))
            return 0xff3000;
        if (s.equals("gr1"))
            return 0xc0ff00;
        if (s.equals("gr2"))
            return 0x80ff00;
        if (s.equals("gr3"))
            return 0x40ff00;
        if (s.equals("str"))
            isStrikethrough = true;
        if (s.equals("end"))
            isStrikethrough = false;
        return -1;
    }

    private void drawGlyph(byte origPixels[], int x, int y, int charWidth, int charHeight, int color) {
        int j1 = x + y * width;
        int k1 = width - charWidth;
        int l1 = 0;
        int i2 = 0;
        if (y < topY) {
            int j2 = topY - y;
            charHeight -= j2;
            y = topY;
            i2 += j2 * charWidth;
            j1 += j2 * width;
        }
        if (y + charHeight >= viewport_h)
            charHeight -= ((y + charHeight) - viewport_h) + 1;
        if (x < topX) {
            int k2 = topX - x;
            charWidth -= k2;
            x = topX;
            i2 += k2;
            j1 += k2;
            l1 += k2;
            k1 += k2;
        }
        if (x + charWidth >= viewport_w) {
            int l2 = ((x + charWidth) - viewport_w) + 1;
            charWidth -= l2;
            l1 += l2;
            k1 += l2;
        }
        if (!(charWidth <= 0 || charHeight <= 0)) {
            colorPixels(pixels, origPixels, color, i2, j1, charWidth, charHeight, k1, l1);
        }
    }

    private void colorPixels(int pixels[], byte origPixels[], int color, int origPixelIndex, int pixelIndex, int charWidth, int charHeight,
                           int pixelIndexIncrement, int origPixelIndexIncrement) {
        int l1 = -(charWidth >> 2);
        charWidth = -(charWidth & 3);
        for (int i2 = -charHeight; i2 < 0; i2++) {
            for (int j2 = l1; j2 < 0; j2++) {
                if (origPixels[origPixelIndex++] != 0)
                    pixels[pixelIndex++] = color;
                else
                    pixelIndex++;
                if (origPixels[origPixelIndex++] != 0)
                    pixels[pixelIndex++] = color;
                else
                    pixelIndex++;
                if (origPixels[origPixelIndex++] != 0)
                    pixels[pixelIndex++] = color;
                else
                    pixelIndex++;
                if (origPixels[origPixelIndex++] != 0)
                    pixels[pixelIndex++] = color;
                else
                    pixelIndex++;
            }

            for (int k2 = charWidth; k2 < 0; k2++)
                if (origPixels[origPixelIndex++] != 0)
                    pixels[pixelIndex++] = color;
                else
                    pixelIndex++;

            pixelIndex += pixelIndexIncrement;
            origPixelIndex += origPixelIndexIncrement;
        }

    }

    private void drawShadowedChar(int alpha, int x, byte origPixels[], int charWidth, int y, int charHeight,
                           int color) {
        int k1 = x + y * width;
        int l1 = width - charWidth;
        int i2 = 0;
        int j2 = 0;
        if (y < topY) {
            int k2 = topY - y;
            charHeight -= k2;
            y = topY;
            j2 += k2 * charWidth;
            k1 += k2 * width;
        }
        if (y + charHeight >= viewport_h)
            charHeight -= ((y + charHeight) - viewport_h) + 1;
        if (x < topX) {
            int l2 = topX - x;
            charWidth -= l2;
            x = topX;
            j2 += l2;
            k1 += l2;
            i2 += l2;
            l1 += l2;
        }
        if (x + charWidth >= viewport_w) {
            int i3 = ((x + charWidth) - viewport_w) + 1;
            charWidth -= i3;
            i2 += i3;
            l1 += i3;
        }
        if (charWidth <= 0 || charHeight <= 0)
            return;
        recolorAlpha(origPixels, charHeight, k1, pixels, j2, charWidth, i2, l1, color, alpha);
    }

    private void recolorAlpha(byte origPixels[], int height, int j, int pixels[], int l, int width,
                           int j1, int k1, int color, int alpha) {
        color = ((color & 0xff00ff) * alpha & 0xff00ff00) + ((color & 0xff00) * alpha & 0xff0000) >> 8;
        alpha = 256 - alpha;
        for (int j2 = -height; j2 < 0; j2++) {
            for (int k2 = -width; k2 < 0; k2++)
                if (origPixels[l++] != 0) {
                    int l2 = pixels[j];
                    pixels[j++] = (((l2 & 0xff00ff) * alpha & 0xff00ff00) + ((l2 & 0xff00) * alpha & 0xff0000) >> 8) + color;
                } else {
                    j++;
                }

            j += k1;
            l += j1;
        }

    }

    private final byte[][] glyphPixels;
    private final int[] glyphWidth;
    private final int[] glyphHeight;
    private final int[] horizontalKerning;
    private final int[] verticalKerning;
    private final int[] charEffectiveWidth;
    public int charHeight;
    private final Random random;
    private boolean isStrikethrough;
}
